import sqlite3

__author__ = 'romilly'

class DatabaseException(Exception):
    pass

class Database:
    def __init__(self, path):
        self._path = path

    def execute(self, command):
        self.cursor.execute(command)

    def commit(self):
        self.connection.commit()

    def executeQuery(self, command, *args):
        self.cursor.execute(command, *args)
        return self.cursor.fetchall()

    def getNewId(self):
        return self.cursor.lastrowid

    def open(self):
        try:
            self.connection = sqlite3.connect(self._path)
            self.cursor = self.connection.cursor()
        except sqlite3.OperationalError: # Can't locate database file
            raise DatabaseException('cannot open database %s' % self._path)

    def close(self):
        if self.cursor is not None:
            self.cursor.close()
            self.cursor = None
        if self.connection is not None:
            self.connection.close()
            self.connection = None


  